#!/usr/bin/env rc

if (! 9p stat acme/index > /dev/null) {
	echo 'def: acme not running' >[1=2];
	exit fail
}

if(~ $#winid 0){
	echo 'def: not run from named acme window' >[1=2]
	exit fail
}

file=`{9p read acme/$winid/tag | sed -e 's/ .*//' -e 1q}
if(~ $#file 0){
	file=''
}

opts=()
debug = n
while(! ~ $#* 0 && ~ $1 -*) {
	if(~ $1 -debug) {
		debug = y
	}
	opts=($opts $1)
	shift
}

addr = `{acmedot}
if(! ~ $#addr 2){
	echo bad addr $addr >[1=2]
	exit fail
}
cmd = (godef $opts -r -o $addr(1)-i $file $*)
if(~ $debug y){
	echo $cmd
}
echo '	'^$file^':#'^$addr(1)
9p read acme/$winid/body | $cmd
